/**
 * jims
 */
package com.jims.master.modules.doctor.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.doctor.entity.ClinicMaster;
import com.jims.master.modules.doctor.entity.PregnantNatalcRecord;
import com.jims.master.modules.doctor.service.ClinicMasterService;
import com.jims.master.modules.doctor.service.PregnantNatalcRecordService;
import com.jims.master.modules.doctor.service.PregnantRecordsService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 孕妇手册-产后42天体检表Controller
 * @author 王志明
 * @version 2017-07-23
 */
@Controller
@RequestMapping(value = "${adminPath}/doctor/pregnantNatalcRecord")
public class PregnantNatalcRecordController extends BaseController {

	@Autowired
	private PregnantNatalcRecordService pregnantNatalcRecordService;
    @Autowired
    private ClinicMasterService clinicMasterService;
    @Autowired
    private PregnantRecordsService pregnantRecordsService;
	@ModelAttribute
	public PregnantNatalcRecord get(@RequestParam(required=false) String id) {
		PregnantNatalcRecord entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = pregnantNatalcRecordService.get(id);
		}
		if (entity == null){
			entity = new PregnantNatalcRecord();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(PregnantNatalcRecord pregnantNatalcRecord,String clinicId, HttpServletRequest request, HttpServletResponse response, Model model) {
        ClinicMaster clinicMaster = clinicMasterService.get(clinicId);
        pregnantNatalcRecord.setClinicId(clinicId);
        Page<PregnantNatalcRecord> page = pregnantNatalcRecordService.findPage(new Page<PregnantNatalcRecord>(request, response), pregnantNatalcRecord);
		model.addAttribute("page", page);
        model.addAttribute("clinicMaster", clinicMaster);
		return "modules/doctor/pregnantManual/pregnantNatalcRecordIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(PregnantNatalcRecord pregnantNatalcRecord, Model model) {
		model.addAttribute("pregnantNatalcRecord", pregnantNatalcRecord);
		return "modules/pregnantnatalcrecord/pregnantNatalcRecordForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(PregnantNatalcRecord pregnantNatalcRecord, Model model) {
	    StringData data=new StringData();
		int i=pregnantNatalcRecordService.save(pregnantNatalcRecord);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(PregnantNatalcRecord pregnantNatalcRecord) {
	    StringData data=new StringData();
		int i=pregnantNatalcRecordService.delete(pregnantNatalcRecord);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
		return data;
	}
    /**
     * @Author chenxy
     * @desc 查看复诊记录
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"findById", ""})
    public String findById(String clinicId,String id, String patientId,HttpServletRequest request, HttpServletResponse response, Model model) {
        ClinicMaster clinicMaster = clinicMasterService.get(clinicId);
        PregnantNatalcRecord pregnantNatalcRecord = this.pregnantNatalcRecordService.get(id);
        String orgId = UserUtils.getOrgId();
        String orgName = pregnantRecordsService.getOrgName(orgId);
        String deptName = UserUtils.getUser().getOrgDept().getDeptName();
        String doctorName = UserUtils.getUser().getName();
        if (pregnantNatalcRecord == null) {
            pregnantNatalcRecord=new PregnantNatalcRecord();
            pregnantNatalcRecord.setTelPhone(clinicMaster.getPatMasterIndex().getPhone());
        }
        model.addAttribute("pregnantNatalcRecord", pregnantNatalcRecord);
        model.addAttribute("clinicMaster", clinicMaster);
        return "modules/doctor/pregnantManual/pregnantNatalcRecordForm";
    }
}